# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render,render_to_response
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.template import loader,RequestContext,Template
from appTest.models import Publisher
from appTest.forms import PublisherForm

# Create your views here.


def test(request,id,key):
	print request.GET.get('name2')
	print 'id:{},key:{}'.format(id,key)
	return render(request,'test.html')

def index(request):
	return render(request,'index.html')

def user(request,id):
	a = id #id值由url配置中参数传入
	user_list = User.objects.all()
	return render(request,'user.html',locals())
	# return render_to_response('user.html', locals(),context=RequestContext(request))
	# return HttpResponse(t.render(c, request), content_type='text/html')
	# return redirect('https://www.baidu.com/')

def add_publisher(request):
	# 1、不使用Form的情况
	# if request.method == 'POST':
		# name = request.POST['name']
		# address = request.POST['address']
		# city = request.POST['city']
		# state_province = request.POST['state_province']
		# country = request.POST['country']
		# website = request.POST['website']
		# Publisher.objects.create(
		# 	name = name,
		# 	address = address,
		# 	city = city,
		# 	state_province = state_province,
		# 	country = country,
		# 	website = website,
		# )
		# return HttpResponse('添加出版社信息成功')
	# else:
	# 	publisher_form = PublisherForm()
	# return render(request,'add_publisher.html',locals())

	# 2、使用Django Form的情况
	# if request.method == 'POST':
		# publisher_form = PublisherForm(request.POST) #实例并指定POST
		# if publisher_form.is_valid(): #验证表单数据是否是合法的，比如是否为空
		# 	Publisher.objects.create(
		# 		name = publisher_form.cleaned_data['name'],
		# 		address = publisher_form.cleaned_data['address'],
		# 		city = publisher_form.cleaned_data['city'],
		# 		state_province = publisher_form.cleaned_data['state_province'],
		# 		country = publisher_form.cleaned_data['country'],
		# 		website = publisher_form.cleaned_data['website'],
		# 	)
		# 	return HttpResponse('添加出版社信息成功')
	# else:
	# 	publisher_form = PublisherForm()
	# return render(request, 'add_publisher.html', locals())

	# 3、使用Django ModelForm的情况
	if request.method == 'POST':
		publisher_form = PublisherForm(request.POST)
		if publisher_form.is_valid():
			publisher_form.save()
			return HttpResponse('添加出版社信息成功')
	else:
		publisher_form = PublisherForm()
	return render(request,'add_publisher.html',locals())
